package cn.edu.sjtu.se.dslab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.sjtu.se.dslab.component.design.Button;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternComponent;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternType;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.service.IButtonService;
import cn.edu.sjtu.se.dslab.service.IDesignPatternComponentService;

@Controller
@RequestMapping(value="/controlDesignPattern")
public class ControlDpConfigController {
	private static final Logger logger = Logger.getLogger(ControlDpConfigController.class);

	@Resource(name="designPatternComponentService")
	IDesignPatternComponentService designPatternComponentService;

	@Resource(name="buttonService")
	IButtonService buttonService;

	@RequestMapping(value = "/{type}/function/add", method = RequestMethod.POST)
	public String addFunctionMapping(@PathVariable(value ="type")String type,  @RequestParam String dpcUri, 
			@RequestParam long projectId, @RequestParam String page,
			@RequestParam String buttonId, @RequestParam String functionId) throws IOException {
		Map<String, String> controlMap = new HashMap<String, String>();
		controlMap.put("page", page);
		controlMap.put("button", buttonId);
		controlMap.put("type", "function");
		controlMap.put("control_function", functionId);
		logger.info(functionId);
		designPatternComponentService.addDpcItem(controlMap, dpcUri);
		return toURL(type, projectId);
	}
	
	
	@RequestMapping(value = "/{type}/jump/add", method = RequestMethod.POST)
	public String addJumpMapping( @PathVariable(value ="type")String type,@RequestParam String dpcUri, 
			@RequestParam long projectId, @RequestParam String page,
			@RequestParam String buttonId, @RequestParam String targetPage) throws IOException {
		Map<String, String> controlMap = new HashMap<String, String>();
		controlMap.put("page", page);
		controlMap.put("button", buttonId);
		controlMap.put("type", "jump");
		controlMap.put("target_page", targetPage);
		logger.info(targetPage);
		
		designPatternComponentService.addDpcItem(controlMap, dpcUri);
		return toURL(type, projectId);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delelteControlMapping(
			@RequestParam long projectId, @RequestParam String mappingId,
			@RequestParam String dpcUri){
		Map<String, String> resultMap = new HashMap<String,String>();
		try{
			designPatternComponentService.deleteDpcItem("id", mappingId, dpcUri);
			resultMap.put("result", Boolean.TRUE.toString());
		}catch(Exception ex){
			resultMap.put("result", Boolean.FALSE.toString());
		}
		return resultMap;
	}

	@RequestMapping(value = "/{type}/addButton", method = RequestMethod.POST)
	public String addButton(Button button, @RequestParam long projectId,
			@PathVariable(value ="type")String type,@RequestParam String dpcUri){
		boolean result = buttonService.addButton(button, projectId);
		return toURL(type, projectId);
	}

	@RequestMapping(value = "/deleteButton", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delelteButton(@RequestParam long projectId, @RequestParam long buttonId,
			@RequestParam String dpcUri){
		Map<String, String> resultMap = new HashMap<String,String>();
		try{
			buttonService.deleteButtonById(buttonId);
			resultMap.put("result", Boolean.TRUE.toString());
		}catch(Exception ex){
			resultMap.put("result", Boolean.FALSE.toString());
		}
		return resultMap;
		//return toURL(type, projectId);
	}

	private String toURL(String type, long projectId){
		//StringBuffer url = new StringBuffer("redirect:../");
		//return url.append(dpcUri.replace(':', '/')).append("?projectId=").append(projectId).toString();
		StringBuilder url = new StringBuilder("redirect:/");
		url.append(type).append("/").append(projectId).append("/design_pattern_component/CONTROL/edit");
		return url.toString();
	}



}
